﻿using System;
using System.ComponentModel;
using System.Globalization;

namespace InfiniTec
{
	public class FullqualifiedTypeNameConverter : TypeConverter
	{
		public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
		{
			return sourceType == typeof(string);
		}

		public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
		{
			return destinationType == typeof(string);
		}

		public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
		{
		    Type result = string.IsNullOrEmpty((string)value) ? null : Type.GetType((string)value, true);
		    return result;
		}

	    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value,
		                                 Type destinationType)
		{
			return (value == null) ? string.Empty : value.GetType().FullName;
		}
	}
}